import os,sys,yaml
pro=os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))))
sys.path.append(os.path.join(pro,'easy_edit','_base_tool',"python"))
from tool_sqlite3 import base_db

print(pro)
class main:
    def __init__(self,dir_path=None,db_name='前端组件数据库'):
        if dir_path is None:
            self.组件存放文件夹=os.path.join(pro,'easy_edit','_web','vue_template_store')
        else:
            self.组件存放文件夹=dir_path
    #检查文件夹是否存在不存在则创建文件夹
    def check_dir(self,user_id,dir1_id):
        #用户目录
        user_dir_path=os.path.join(self.组件存放文件夹,'a'+str(user_id),"a"+str(dir1_id))
        yaml_path=os.path.join(user_dir_path,'main.yaml')
        if not os.path.exists(user_dir_path):
            try:
                os.mkdir(user_dir_path)
                with open(yaml_path, 'w',encoding='utf-8') as file:  
                    yaml.dump({"dir1_index":0}, file, default_flow_style=False, allow_unicode=True)  
            except:pass
        return yaml_path
    def run(self,user_id,dir1_id,dir2_name):
        #-1获取目录1 路径
        dir1_path=os.path.join(self.组件存放文件夹,'a'+str(user_id),"a"+str(dir1_id))
        #-2获取目录1配置
        yaml_path=os.path.join(dir1_path,'main.yaml')
        #-3读取目录1配置下的目录2id
        with open(yaml_path, 'r',encoding='utf-8') as file:  
            data = yaml.safe_load(file)
        data['dir2_index']+=1
        #-4更新目录1配置
        with open(yaml_path, 'w',encoding='utf-8') as file:  
            yaml.dump(data, file, default_flow_style=False, allow_unicode=True)  
        #-4创建目录2
        mkdir_path=os.path.join(dir1_path,'a'+str(data['dir2_index']))
        os.mkdir(mkdir_path)
        #-4创建目录2配置文件
        mkfile_path=os.path.join(mkdir_path,'main.yaml')
        write_data={"name":dir2_name,'temp_index':0,"dir2_id":str(data['dir2_index'])}
        with open(mkfile_path,'w',encoding='utf-8')as f:
            yaml.dump(write_data, f, default_flow_style=False, allow_unicode=True)
        return {"type":"success","data":write_data}
if __name__=="__main__":    
    obj=main()
    obj.run('2','1','测试')